package com.jeecg.p3.system.poster.web;

import com.jeecg.p3.base.util.DataGrid;
import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.base.util.StringUtils;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.entity.Product;
import com.jeecg.p3.system.poster.entity.ExamAnswer;
import com.jeecg.p3.system.poster.entity.ExamPaper;
import com.jeecg.p3.system.poster.entity.ExamQuestionBank;
import com.jeecg.p3.system.poster.service.ExamAnswerService;
import com.jeecg.p3.system.poster.service.ExamPaperService;
import com.jeecg.p3.system.poster.service.ExamQuestionBankService;
import com.jeecg.p3.system.service.ProductService;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 *
 * @ClassName: ExamPaperController
 * @Description: TODO 试题控制层
 * @author: zhangxiao
 * @date: 2020年12月8日 下午7:29:38
 */
@Controller
@RequestMapping("/system/back/poster/examPaper")
public class ExamPaperController extends BaseController {
    public final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ExamPaperService service;
    @Autowired
    private ExamQuestionBankService examQuesBankService;
    @Autowired
    private ExamAnswerService answerService;

    /**
     *
     * @param query 分页查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute ExamPaper query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        PageQuery<ExamPaper> pageQuery = new PageQuery<>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        pageQuery.setQuery(query);
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("examPaper",query);
        List<ExamQuestionBank> banks = examQuesBankService.findByParam(null);
        velocityContext.put("banks", banks);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(service.queryPageList(pageQuery)));
        String viewName = "system/back/exam/exam_paper_list.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        List<ExamQuestionBank> banks = examQuesBankService.findByParam(null);
        velocityContext.put("banks", banks);
        String viewName = "system/back/exam/exam_paper_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public AjaxJson doAdd(@ModelAttribute ExamPaper examPaper, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            j = service.doAdd(examPaper, user);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value="/toEdit",method = RequestMethod.GET)
    public void toEdit(@RequestParam(required = true, value = "id" ) String id,HttpServletResponse response,
                       HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        ExamPaper examPaper = service.findById(id);
        ExamAnswer answer = new ExamAnswer();
        answer.setExamPaperId(id);
        List<ExamAnswer> answers = answerService.findByParam(answer);//获取该试题答案列表
        examPaper.setAnswerList(answers);//传答案
        velocityContext.put("examPaper", examPaper);
        List<ExamQuestionBank> banks = examQuesBankService.findByParam(null);
        velocityContext.put("banks", banks);
        String viewName = "system/back/exam/exam_paper_edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存修改数据
     * @return
     */
    @RequestMapping(value = "/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute ExamPaper examPaper, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            /*examPaper.setUpdateDate(DateUtil.dateToString(new Date()));
            examPaper.setUpdateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            examPaper.setUpdateUser(getCurrentUser(request).getId());
            this.service.doEdit(examPaper);
            j.setMsg("编辑成功");*/
            j = this.service.doEdit(examPaper, getCurrentUser(request));
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }
        return j;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) String id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            ExamPaper examPaper = service.findById(id);
            examPaper.setDeleteDate(DateUtil.dateToString(new Date()));
            examPaper.setDeleteTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            examPaper.setDeleteUser(user.getId());
            service.doDelete(examPaper);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败:"+e.getMessage());
        }
        return j;
    }

    /**
     * 分页查询试题
     * @param paperName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/pages",method = RequestMethod.POST)
    @ResponseBody
    public DataGrid pages(String paperName, String productId,
        @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
        @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize)
    {
        ExamPaper paper = new ExamPaper();
        paper.setName(paperName);
        if(StringUtils.isNotEmpty(productId)) {
            ExamQuestionBank bank = new ExamQuestionBank();
            Product product = new Product();
            product.setId(Long.valueOf(productId));
            bank.setProduct(product);
            paper.setQuesBank(bank);
        }
        PageQuery<ExamPaper> pageQuery = new PageQuery<>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        pageQuery.setQuery(paper);
        return service.pages(pageQuery);
    }
}
